package com.cy.stone.system.domain.clientobject.role;

import com.cy.stone.system.infrastructure.database.entity.RoleMenuEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色权限入参
 * @author Wings
 * @since 2023-12-10
 */
@Data
@Schema(name = "角色权限关联入参")
public class RoleMenuCO {

    @Schema(description = "角色ID", requiredMode = Schema.RequiredMode.REQUIRED)
    @NotEmpty(message = "角色ID不能为空")
    private String roleId;

    @Schema(description = "角色码", requiredMode = Schema.RequiredMode.REQUIRED)
    @NotEmpty(message = "角色码不能为空")
    private String roleCode;

    @Schema(description = "关联的菜单", requiredMode = Schema.RequiredMode.REQUIRED)
    @Valid
    private List<MenuCO> menus;

    public static List<RoleMenuEntity> toEntities(RoleMenuCO roleMenuCO) {
        List<RoleMenuEntity> list = new ArrayList<>();
        for(MenuCO menuCO: roleMenuCO.menus) {
            RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
            roleMenuEntity.setRoleId(roleMenuCO.getRoleId());
            roleMenuEntity.setRoleCode(roleMenuCO.getRoleCode());
            roleMenuEntity.setMenuId(menuCO.getMenuId());
            roleMenuEntity.setMenuCode(menuCO.getMenuCode());
            list.add(roleMenuEntity);
        }
        return list;
    }
}
